/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.luxoft.corporate.blog.ui;

import com.luxoft.corporate.blog.db.Controller;
import com.luxoft.corporate.blog.db.Query;
import com.luxoft.corporate.blog.db.QueryImpl;
import com.luxoft.corporate.blog.db.entities.User;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingWorker;

/**
 *
 * @author Egor
 */
public class SiginForm extends JPanel {

    private JTextField userNameField;
    private JLabel userNameLabel;

    private JPasswordField userPasswordField;
    private JLabel userPasswordLabel;

    private JButton signInButton;
    private JButton registerButton;
    private JCheckBox isAuthor;
    private Controller<User> controller;
    private List<User> users;

    private volatile boolean isDone = false;
    public SiginForm() {
        users = new LinkedList<>();
        isAuthor = new JCheckBox("I'm author");
        userNameField = new JTextField();
        userPasswordField = new JPasswordField();
        userNameLabel = new JLabel("User name: ");
        userPasswordLabel = new JLabel("User password: ");
        signInButton = new JButton("Sigin");
        registerButton = new JButton("Register");
        init();
    }

    private void init() {
        registerButton.setEnabled(false);

        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        Box userNameBox = Box.createHorizontalBox();
        userNameBox.add(userNameLabel);
        userNameBox.add(Box.createHorizontalStrut(5));
        userNameBox.add(userNameField);

        Box userPasswordBox = Box.createHorizontalBox();
        userPasswordBox.add(userPasswordLabel);
        userPasswordBox.add(Box.createHorizontalStrut(5));
        userPasswordBox.add(userPasswordField);

        Box buttonsBox = Box.createHorizontalBox();
        buttonsBox.add(Box.createHorizontalGlue());
        buttonsBox.add(isAuthor);
        buttonsBox.add(Box.createHorizontalStrut(5));
        buttonsBox.add(signInButton);
        buttonsBox.add(Box.createHorizontalStrut(5));
        buttonsBox.add(registerButton);

        add(Box.createVerticalStrut(5));
        add(userNameBox);
        add(Box.createVerticalStrut(5));
        add(userPasswordBox);
        add(Box.createVerticalStrut(5));
        add(buttonsBox);
        add(Box.createVerticalStrut(5));

        signInButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingWorker<List<User>, Object> sw = new SwingWorker<List<User>, Object>() {

                    @Override
                    protected List<User> doInBackground() throws Exception {

                        isDone = false;
                        Query query = new QueryImpl.QueryBuilder(Query.SupportedRequestTypes.SELECT).addSqlCommand("*").
                                addSqlCommand("FROM blog_useres").addSqlCommand("WHERE").
                                addSqlCommand("u_nick =").addStringParametr(userNameField.getText()).
                                addSqlCommand("AND").addSqlCommand("u_password =").
                                addStringParametr(String.valueOf(userPasswordField.getPassword())).build();

                        System.out.println(" >>>> " + query);
                        users = controller.getData(query);
                        System.out.println(">>> User Count " + users.size());
                        return users;
                    }

                    @Override
                    protected void done() {
                        isDone = true;
                        registerButton.setEnabled(users.isEmpty());
                    }

                };
                sw.execute();
            }
        });

        registerButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                SwingWorker<User, Object> sw = new SwingWorker<User, Object>() {

                    @Override
                    protected User doInBackground() throws Exception {

                        Query query = new QueryImpl.QueryBuilder(Query.SupportedRequestTypes.INSERT).
                                addSqlCommand("blog_useres (u_nick, u_password, u_isAuthor)").addSqlCommand("VALUES").
                                addSqlCommand("(").addStringParametr(userNameField.getText()).addSqlCommand(",").
                                addStringParametr(String.valueOf(userPasswordField.getPassword())).addSqlCommand(",").addBooleanParameter(isAuthor.isSelected()).addSqlCommand(")").build();

                        System.out.println(">>>>>  " + query);
                        return controller.addData(query);
                    }

                };
                sw.execute();
            }
        });

    }

    public List<User> getUsers() {
        return users;
    }

    public void setController(Controller controller) {
        this.controller = controller;
    }

    public boolean isDone() {
        return isDone;
    }

    
    
    
}
